package com.hom;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

//本类用于演示socket通讯单次的双向传递对象
public class Test3 {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 9999);
            User user = new User();
            Scanner input = new Scanner(System.in);
            System.out.println("请输入用户名");
            user.setLoginName(input.next());
            System.out.println("请输入密码");
            user.setLoginPwd(input.next());
            user.setUserID(1);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(user);
            oos.flush();
            socket.shutdownOutput();
            //读取字符串
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String answer = br.readLine();
            if("success".equals(answer)){

                System.out.println("恭喜你，登录成功");
            }else{
                System.out.println("用户名或密码错误，请重新输入");
            }
            br.close();
            socket.close();


        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
